package com.niiwoo.civet.trade.service.local.loan.submit;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.dao.entity.SmallLoanWhiteUsers;
import com.niiwoo.civet.trade.dto.request.loan.LoanSubmitReqDTO;
import com.niiwoo.civet.trade.dto.response.loan.LoanConfigResDTO;
import com.niiwoo.civet.trade.dto.response.loan.LoanSubmitCommResDTO;
import com.niiwoo.civet.trade.enums.LoanLimitMessageEnum;
import com.niiwoo.civet.trade.enums.LoanSubmitTypeEnum;
import com.niiwoo.civet.trade.enums.ProjectStatusEnum;
import com.niiwoo.civet.trade.enums.ProjectTypeEnum;
import com.niiwoo.civet.trade.service.local.loan.LoanBusinessLocalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * @Author: wuke
 * @Description: 小易贷借款提交业务处理类
 * @Date: Created in 17:13 2018/4/13
 */
@Slf4j
@Service
public class SmallLoanSubmitService extends AbstractLoanSubmitService<LoanSubmitReqDTO,LoanSubmitCommResDTO> {
    @Autowired
    private LoanBusinessLocalService loanBusinessLocalService;

    @Override
    public void intoCheck(LoanSubmitReqDTO req, LoanSubmitCommResDTO res) {
//        smallLoanWhiteCheck(req, res);
        super.intoCheck(req, res);
    }

    @Override
    public void submitCheck(LoanSubmitReqDTO req, LoanSubmitCommResDTO res) {
//        smallLoanWhiteCheck(req, res);
        super.submitCheck(req, res);
    }

    //白名单检查
    private void smallLoanWhiteCheck(LoanSubmitReqDTO req, LoanSubmitCommResDTO res){
        String userId = req.getUserId();
        SmallLoanWhiteUsers smallLoanWhiteUsers = loanBusinessLocalService.querySmallLoanWhite(userId);
        log.info("smallLoanWhiteCheck.userId={},smallLoanWhiteUsers={}",userId, JSON.toJSONString(smallLoanWhiteUsers));
        if(smallLoanWhiteUsers==null){
            excetipnResponseDeal(LoanLimitMessageEnum.LIMIT_10048,res);
        }
    }

    /*@Override
    public void commonCheck(LoanSubmitReqDTO req, LoanSubmitCommResDTO res) {
        //如果存在申请成功过其它标的，则不允许申请小易贷
        isExistOtherProjectCheck(req,res);
        super.commonCheck(req, res);
    }*/

    //检查是否存在其它标的---(5.1.6版本废弃)
    @Deprecated
    private void isExistOtherProjectCheck(LoanSubmitReqDTO req, LoanSubmitCommResDTO res){
        //查询其它成功标的数量
        int otherProjectCount = projectMapper.selectCountNoContainTypeEnum(
                req.getUserId(),
                ProjectTypeEnum.SMALL_LOAN,
                Arrays.asList(ProjectStatusEnum.REPAYING, ProjectStatusEnum.FINISH)
        );
        log.info("isExistOtherProjectCheck.otherProjectCount={}",otherProjectCount);
        if(otherProjectCount>0){
            excetipnResponseDeal(LoanLimitMessageEnum.LIMIT_10048,res);
        }
    }

    @Override
    protected LoanSubmitTypeEnum getLoanSubmitMapEnum() {
        return LoanSubmitTypeEnum.SMALL_LOAN;
    }
}
